//
//  ViewController.m
//  Find
//
//  Created by Eason on 14-12-10.
//  Copyright (c) 2014年 Eason. All rights reserved.
//

#import "ViewController.h"
#import "News.h"
#define kTime 0.5
@interface ViewController ()
@end

@implementation ViewController {
    NSString *loserID;
    NSString *loserName;
    NSString *loserphoneNumber;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    _cardID.delegate  = self;
    _cardName.delegate = self;
    _phoneNumber.delegate = self;
    _cardID .tag = 1;
    _cardName.tag = 2;
    _phoneNumber.tag = 3;
    
    //请求后台并且发送秘钥
    [Bmob registerWithAppKey:@"c360e4f9b11fbe30a5eead1d2c264266"];
    
  
}

- (IBAction)submit:(id)sender {
    
    loserID  = _cardID.text;
    loserName = _cardName.text;
    loserphoneNumber = _phoneNumber.text;
    
    BmobObject *gameScore = [BmobObject objectWithClassName:@"GameScore"];//指明加入的表单
    [gameScore setObject:loserName forKey:@"cardName"]; //将名字输入框的文字上传到cardName
    [gameScore setObject:loserID forKey:@"cardID"]; //将卡ID上传
    [gameScore setObject:loserphoneNumber forKey:@"phone"]; //将卡phoneNumer上传
    
    [gameScore setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];
    [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        
        //上传成功显示一个alertView,并且清空textfield中的文字
        UIAlertView * alterView = [[UIAlertView alloc]initWithTitle:@"成功上传好人一生平安" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Enter", nil];
        
        [alterView show];
        //清空文本框中的文字
        _cardID.text  = nil;
        _cardName.text = nil;
        _phoneNumber.text = nil;
        //收起键盘
        [_cardName resignFirstResponder];
        [_cardID  resignFirstResponder];
        [_phoneNumber resignFirstResponder];
        
    }];
}
//按下文本框键盘上的 return触发的事件
- (IBAction)touchReturn:(UITextField *)textField {

    if(textField.tag == 1)
        [_cardName becomeFirstResponder];
    if(textField.tag == 2)
        [_phoneNumber becomeFirstResponder];
    else
        //让 return触发 submitBut按钮的点击事件
        [self.submitBnt sendActionsForControlEvents:UIControlEventTouchUpInside];
}
//键盘处理
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [_cardName resignFirstResponder];
    [_cardID  resignFirstResponder];
    [_phoneNumber resignFirstResponder];
}

//输入时将文本框上拉升 防止挡住
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect tempRect = self.view.frame;
    tempRect.origin.y -= 50;
    [UIView animateWithDuration:kTime animations:^{
        self.view.frame = tempRect;
    }];
    
}
//结束textField编辑时view恢复原状
-(void)textFieldDidEndEditing:(UITextField *)textField{
  
  self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
 
}
@end
